package com.cxx.service.ServiceImpl;

import com.cxx.mapper.BlogsMapper;
import com.cxx.pojo.Blog;
import com.cxx.pojo.Comments;
import com.cxx.pojo.Replay;
import com.cxx.service.BlogsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogsServiceImpl implements BlogsService {
    @Autowired
    private BlogsMapper blogsMapper;
    @Override
    public void addBlogs(Blog blog) {
        blogsMapper.addBlogs(blog);
    }

    @Override
    public List<Blog> blogs(String account) {
        return blogsMapper.blogs(account);
    }

    @Override
    public List<Blog> blogsByTag(String account, String tag) {
        return  blogsMapper.blogsByTag(account,tag);
    }

    @Override
    public List<Blog> blogsList() {
        return blogsMapper.blogsList();
    }

    @Override
    public Blog blogById(Integer id) {
        return blogsMapper.blogById(id);
    }

    @Override
    public void writeComment(Comments comments) {
        blogsMapper.writeComment(comments);
        List<Comments> commentsList=blogsMapper.getAllComment();
        for (Comments comment : commentsList) {
            if(!blogsMapper.existComment(comment.getId())){
                String toCommentAccount=blogsMapper.blogById(comment.getBlogId()).getAccount();
                if(!comment.getAccount().equals(toCommentAccount)){
                    blogsMapper.insertReadComment(comment.getId(),comment.getAccount(),toCommentAccount);
                }
            }
        }
    }

    @Override
    public List<Comments> getComments(Integer id) {
        return blogsMapper.getComments(id);
    }

    @Override
    public void writeReplay(Replay replay) {
        blogsMapper.writeReplay(replay);
        List<Replay> replaysList=blogsMapper.getAllReplay();
        for (Replay replayItem : replaysList) {
            if(!blogsMapper.existReplay(replayItem.getId())){
                if(!replayItem.getAccount().equals(replayItem.getToAccount())){
                    blogsMapper.insertReadReplay(replayItem.getId(),replayItem.getAccount(),replayItem.getToAccount());
                }
            }
        }
    }
}
